package com.houfukude.Calculate;

import java.util.HashMap;
import java.util.List;

import com.houfukude.xtestmgr.R;

import android.R.integer;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class InputListAdapter extends BaseAdapter {
	private List<InputValue> list = null;
	private InputView input = null;
	private Context context;

	public InputListAdapter(List<InputValue> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {

		input = new InputView();
		view = LayoutInflater.from(context).inflate(R.layout.list_input, null);
		input.setTitle((TextView) view.findViewById(R.id.input_title));
		input.setLabel_x((TextView) view.findViewById(R.id.label_x));
		input.setValue_x((EditText) view.findViewById(R.id.value_x));
		input.setLabel_y((TextView) view.findViewById(R.id.label_y));
		input.setValue_y((EditText) view.findViewById(R.id.value_y));

		input.getTitle().setText(list.get(position).getTitle());

		if (list.get(position).getExistY() != 0) {
			input.getLabel_y().setVisibility(View.GONE);
			input.getValue_y().setVisibility(View.GONE);
		}

		TextWatcher textWatcher_x = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				list.get(position).setValue_x(s.toString());
			}
		};
		TextWatcher textWatcher_y = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				list.get(position).setValue_y(s.toString());
			}
		};
		input.getValue_x().addTextChangedListener(textWatcher_x);

		input.getValue_y().addTextChangedListener(textWatcher_y);

		if (list.get(position).getLable_x() != null) {
			input.getLabel_x().setText(list.get(position).getLable_x());
		}
		if (list.get(position).getLable_y() != null) {
			input.getLabel_y().setText(list.get(position).getLable_y());
		}

		view.setTag(list.get(position));

		return view;

	}

}
